home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / pbmplus / pgm / pgmtofs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.2 KB  |  153 lines

  1. /* pgmtofs.c - convert portable graymap to Usenix FaceSaver(tm) format
  2. **
  3. ** Copyright (C) 1991 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pgm.h"
  14.  
  15. static void putinit ARGS(( int cols, int rows, int bps ));
  16. static void putitem ARGS(( void ));
  17. static void putgray ARGS(( gray g ));
  18. static void putrest ARGS(( void ));
  19.  
  20. void
  21. main( argc, argv )
  22. int argc;
  23. char* argv[];
  24.     {
  25.     FILE* ifp;
  26.     gray** grays;
  27.     register gray* gP;
  28.     int argn, rows, cols, bps, padright, row, col;
  29.     gray maxval, nmaxval;
  30.     char* usage = "[pgmfile]";
  31.  
  32.     pgm_init( &argc, argv );
  33.  
  34.     argn = 1;
  35.  
  36.     if ( argn < argc )
  37.     {
  38.     ifp = pm_openr( argv[argn] );
  39.     ++argn;
  40.     }
  41.     else
  42.     {
  43.     ifp = stdin;
  44.     }
  45.  
  46.     if ( argn != argc )
  47.     pm_usage( usage );
  48.  
  49.     grays = pgm_readpgm( ifp, &cols, &rows, &maxval );
  50.     pm_close( ifp );
  51.  
  52.     /* Figure out bps. */
  53.     bps = pm_maxvaltobits( (int) maxval );
  54.     if ( bps > 2 && bps < 4 )
  55.     bps = 4;
  56.     else if ( bps > 4 && bps < 8 )
  57.     bps = 8;
  58.     else if ( bps > 8 )
  59.     pm_error(
  60.         "maxval of %d is too large for FaceSaver(tm)", maxval );
  61.     nmaxval = pm_bitstomaxval( bps );
  62.     
  63.     /* Compute padding to round cols * bps up to the nearest multiple of 8. */
  64.     padright = ( ( cols * bps + 7 ) / 8 ) * 8 - cols * bps;
  65.  
  66.     putinit( cols, rows, bps );
  67.     for ( row = rows - 1; row >= 0; --row )
  68.     {
  69.         for ( col = 0, gP = grays[row]; col < cols; ++col, ++gP )
  70.         {
  71.         if ( maxval != nmaxval )
  72.         *gP = (int) *gP * nmaxval / maxval;
  73.         putgray( *gP );
  74.         }
  75.     for ( col = 0; col < padright; ++col )
  76.         putgray( 0 );
  77.         }
  78.  
  79.     putrest( );
  80.  
  81.     exit( 0 );
  82.     }
  83.  
  84.  
  85. static int bitspersample, item, bitsperitem, bitshift, itemsperline, items;
  86.  
  87. static void
  88. putinit( cols, rows, bps )
  89. int cols, rows, bps;
  90.     {
  91.     printf( "FirstName: \n" );
  92.     printf( "LastName: \n" );
  93.     printf( "E-mail: \n" );
  94.     printf( "Telephone: \n" );
  95.     printf( "Company: \n" );
  96.     printf( "Address1: \n" );
  97.     printf( "Address2: \n" );
  98.     printf( "CityStateZip: \n" );
  99.     printf( "Date: \n" );
  100.     printf( "PicData: %d %d %d\n", cols, rows, bps );
  101.     printf( "Image: %d %d %d\n", cols, rows, bps );
  102.     printf( "\n" );
  103.  
  104.     bitspersample = bps;
  105.     itemsperline = items = 0;
  106.     item = 0;
  107.     bitsperitem = 0;
  108.     bitshift = 8 - bitspersample;
  109.     }
  110.  
  111. static void
  112. putitem( )
  113.     {
  114.     char* hexits = "0123456789abcdef";
  115.  
  116.     if ( itemsperline == 30 )
  117.     {
  118.     putchar( '\n' );
  119.     itemsperline = 0;
  120.     }
  121.     putchar( hexits[item >> 4] );
  122.     putchar( hexits[item & 15] );
  123.     ++itemsperline;
  124.     ++items;
  125.     item = 0;
  126.     bitsperitem = 0;
  127.     bitshift = 8 - bitspersample;
  128.     }
  129.  
  130. #if __STDC__
  131. static void
  132. putgray( gray g )
  133. #else /*__STDC__*/
  134. static void
  135. putgray( g )
  136. gray g;
  137. #endif /*__STDC__*/
  138.     {
  139.     if ( bitsperitem == 8 )
  140.     putitem( );
  141.     item += g << bitshift;
  142.     bitsperitem += bitspersample;
  143.     bitshift -= bitspersample;
  144.     }
  145.  
  146. static void
  147. putrest( )
  148.     {
  149.     if ( bitsperitem > 0 )
  150.     putitem( );
  151.     printf( "\n" );
  152.     }
  153.